package com.yhny.lykj.yuhannongye.Adapter;

import android.content.res.Resources;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.longsichao.lscframe.view.LSCImageView;
import com.longsichao.slideview.adapter.StaticPagerAdapter;
import com.yhny.lykj.yuhannongye.MyApplication;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.model.PosterListModel;
import com.yhny.lykj.yuhannongye.tools.LCScreenUtils;

import java.util.List;

/**
 * SlideViewAdapter
 * Created by LSC on 2015/8/6.
 */
public class SlideViewAdapter extends StaticPagerAdapter {
    private List<PosterListModel.ListEntity> models;
    private OnItemClickListener listener = null;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public SlideViewAdapter(List<PosterListModel.ListEntity> list) {
        models = list;
    }

    @Override
    public View getView(ViewGroup viewGroup, final int position) {
        LSCImageView view = new LSCImageView(viewGroup.getContext());
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        view.setScaleType(ImageView.ScaleType.FIT_XY);
        Resources resources = view.getContext().getResources();
        int width = LCScreenUtils.getScreenWidth(MyApplication.getContext());
        int height = resources.getDimensionPixelOffset(R.dimen.item_image_height);
        if (!TextUtils.isEmpty(models.get(position).img)) {
            view.setPlaceHolder(R.drawable.normal_pic).setImageURI(Uri.parse(models.get(position).img));
        }else{
            view.setTargetSize(width,height).setImageResource(R.drawable.normal_pic);
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(position);
                }
            }
        });
        return view;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public int getCount() {
        return models.size();
    }
}
